package com.briup.cms.web.controller;

import com.briup.cms.bean.CmsUser;
import com.briup.cms.bean.LoginParam;
import com.briup.cms.service.ICmsUserService;
import com.briup.cms.util.JwtUtil;
import com.briup.cms.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@Api(tags = "登录模块")
@RestController
public class LoginController {
    @Autowired
    private ICmsUserService userService;

    @ApiOperation(value = "登录", notes = "需要提供用户名和密码")
    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginParam param) {
        //对密码加密与数据库进行比对
        CmsUser user = userService.login(param.getUsername(), param.getPassword());
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId",user.getId());
        System.out.println(map);
        //新增token 相关代码
        String token = JwtUtil.generateJwt(map);
        return Result.success(token);
    }
    @ApiOperation(value = "退出登录")
    @PostMapping(value = "/logout")
    public Result logout() {
        return Result.success();
    }

}